//package com.example.wm.config;
//
//import io.github.resilience4j.ratelimiter.RateLimiter;
//import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
//import io.vavr.control.Try;
//import lombok.extern.slf4j.Slf4j;
//import org.jetbrains.annotations.NotNull;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.annotation.Order;
//import org.springframework.web.server.ServerWebExchange;
//import org.springframework.web.server.WebFilter;
//import org.springframework.web.server.WebFilterChain;
//import reactor.core.publisher.Mono;
//
//@Slf4j
//@Configuration
//@Order(1)
//public class GlobalRateLimiterFilter {
//
//    @Autowired
//    private RateLimiterRegistry rateLimiterRegistry;
//
//    @Bean
//    public WebFilter rateLimiterFilter() {
//        RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("globalLimiter");
//
//        return new WebFilter() {
//            @Override
//            public @NotNull Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
//                // 尝试获取许可
//                Try<Void> tryAcquire = Try.run(rateLimiter::acquirePermission);
//
//                if (tryAcquire.isFailure()) {
//                    log.error("请求频率超过限制", tryAcquire.getCause());
//                    return Mono.error(new RuntimeException("请求频率超过限制"));
//                } else {
//                    log.info("请求频率未超过限制，继续处理请求");
//                }
//
//                return chain.filter(exchange);
//            }
//        };
//    }
//}